package com.freak.wanandroid.model.search.detail;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.freak.httpmanager.BaseViewModel;
import com.freak.httpmanager.callback.ApiCallback;
import com.freak.httpmanager.callback.SubscriberCallBack;
import com.freak.wanandroid.bean.SearchDetailBean;
import com.freak.wanandroid.net.CommonLiveData;

/**
 * Created by 74099 on 2018/9/6.
 */

public class SearchDetailViewModel extends BaseViewModel<SearchDetailRepository> {
    protected MutableLiveData<CommonLiveData> mDoCollect = new MutableLiveData<>();
    protected MutableLiveData<CommonLiveData> mDoCancelCollect = new MutableLiveData<>();
    protected MutableLiveData<SearchDetailBean> mGetSearchDetail = new MutableLiveData<>();
    protected MutableLiveData<String> mGetSearchDetailError = new MutableLiveData<>();

    public SearchDetailViewModel(@NonNull Application application) {
        super(application);
    }

    void getSearchDetail(int page, String k) {
        mRepository.getSearchDetail(page, k, new SubscriberCallBack<>(new ApiCallback<SearchDetailBean>() {
            @Override
            public void onSuccess(SearchDetailBean model) {
                mGetSearchDetail.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetSearchDetailError.postValue(msg);
            }
        }));
    }

    void doCancelCollect(int id) {
        mRepository.doCancelCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }

    void doCollect(int id) {
        mRepository.doCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }
}
